<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '350px',
    },
    autoResize: {
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        color: ['rgb(90,177,239)'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999',
            },
          },
        },
        toolbox: {
          x: 'right',
          bottom: 20,
          feature: {
            // dataView: { show: true, readOnly: false },
            // magicType: { show: true, type: ['line', 'bar'] },
            // restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 60,
          top: 60,
          containLabel: true,
        },
        // legend: {
        //   left: 'center',
        //   bottom: 20,
        //   data: ['实际完成工单数', '完成率'],
        // },
        color: ['rgb(105,161,249)', 'rgb(89,205,170)'],
        xAxis: [
          {
            type: 'category',
            data: ['已收款', '未收款'],
            axisPointer: {
              type: 'shadow',
            },
            axisLine: {
              lineStyle: {
                color: '#000',
              },
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
            name: '数量',
            min: 0,
            max: 250,
            interval: 50,
            axisLabel: {
              formatter: '{value}',
            },
            axisLine: {
              lineStyle: {
                color: '#000',
              },
            },
          },
        ],
        series: [
          {
            name: '实际完成工单数',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value
              },
            },
            data: [100, 30],
          },
        ],
      })
    },
  },
}
</script>
